#import "UserPreferences.h"
#import "RSTAPIClient.h"
//#import "NSDictionary+Extensions.h"
//#import "NSDate+Nibware.h"

static NSString* const kUserRootKey = @"user_root_key";

static NSString* const kUserStatus_Active = @"active";
static NSString* const kUserStatus_Verified = @"verified";
static NSString* const kUserStatus_Suspended = @"suspended";

@implementation UserPreferences

+ (id)preferenceValue:(NSString*)key {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    return [[userDefaults objectForKey:kUserRootKey] objectForKey:key];
}

+ (void)setPreferenceValue:(NSString*)key value:(id)value {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *oldPrefs = [userDefaults objectForKey:kUserRootKey];
    if (oldPrefs != nil) {
        NSMutableDictionary *newPrefs = [NSMutableDictionary dictionaryWithDictionary:oldPrefs];
        newPrefs[key] = value;
        [userDefaults setObject:newPrefs forKey:kUserRootKey];
        [userDefaults synchronize];
    }
}
//
//+ (void)clear {
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    [userDefaults setObject:@{} forKey:kUserRootKey];
//    [userDefaults synchronize];
//}
//
//+ (NSUInteger64)userId {
//    return [[self preferenceValue:@"user_id"] unsignedLongLongValue];
//}
//
//+ (void)setUserId:(NSUInteger64)uid {
//    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//    [userDefaults setObject:@{@"user_id": @(uid)} forKey:kUserRootKey];
//    [userDefaults synchronize];
//}

+ (NSString*)rstToken {
    NSString* t = [self preferenceValue:@"rst_token"];
    return t ? t : @"";
}

+ (void)setRstToken:(NSString*)token {
    [self setPreferenceValue:@"rst_token" value:token];
}

//+ (NSString*)version {
//    NSString* t = [self preferenceValue:@"version"];
//    return t ? t : @"";
//}
//
//+ (void)setVersion:(NSString*)version {
//    [self setPreferenceValue:@"version" value:version];
//}
//
//+ (NSString*)screenName {
//    NSString* t = [self preferenceValue:@"screen_name"];
//    return t ? t : @"";
//}
//
//+ (void)setScreenName:(NSString*)name {
//    [self setPreferenceValue:@"screen_name" value:name];
//}
//
//+ (NSString*)avatarUrl {
//    NSString* t = [self preferenceValue:@"avatar_url"];
//    return t ? t : @"";
//}
//
//+ (void)setAvatarUrl:(NSString*)url {
//    [self setPreferenceValue:@"avatar_url" value:url];
//}
//
//+ (NSUInteger64)syncVersion {
//    return [[self preferenceValue:@"sync_version"] unsignedLongLongValue];
//}
//
//+ (void)setSyncVersion:(NSUInteger64)version {
//    [self setPreferenceValue:@"sync_version" value:@(version)];
//}
//
//+ (NSString*)creditStatus {
//    NSString* t = [self preferenceValue:@"credit_status"];
//    return t ? t : @"";
//}
//
//+ (void)setCreditStatus:(NSString*)status {
//    [self setPreferenceValue:@"credit_status" value:status];
//}
//
//+ (BOOL)haveWritePermission {
//    NSString *status = [self creditStatus];
//    return [status isEqualToString:kUserStatus_Active] || [status isEqualToString:kUserStatus_Verified];
//}
//
//+ (BOOL)haveInviteCodePermission {
//    NSString *status = [self creditStatus];
//    return [status isEqualToString:kUserStatus_Verified];
//}
//
//+ (void)luckyMeSurvived {
//    [UserPreferences setCreditStatus:kUserStatus_Verified];
//}
//
//+ (BOOL)needVerification {
//    NSString *status = [self creditStatus];
//    return [status isEqualToString:kUserStatus_Active];
//}
//
//+ (BOOL)needActivate {
//    NSString *status = [self creditStatus];
//    return [status isEqualToString:kUserStatus_Suspended];
//}
//
//+ (NSUInteger)schooldId {
//    return [[self preferenceValue:@"schoold_id"] unsignedIntegerValue];
//}
//
//+ (void)setSchooldId:(NSInteger)sid {
//    [self setPreferenceValue:@"schoold_id" value:@(sid)];
//}
//
//+ (NSString*)schoolName {
//    NSString* t = [self preferenceValue:@"school_name"];
//    return t ? t : @"";
//}
//
//+ (void)setSchoolName:(NSString*)name {
//    [self setPreferenceValue:@"school_name" value:name];
//}
//
//+ (void)saveUserProfile:(id)data {
//    [UserPreferences setUserId:[data[@"id"] longLongValue]];
//    NSString *token = data[@"access_token"];
//    [UserPreferences setRstToken:token];
//    [[RSTAPIClient sharedClient] setAuthorizationHeaderWithToken:token];
//    
//    [UserPreferences setScreenName:data[@"username"]];
//    [UserPreferences setCreditStatus:data[@"status"]];
//    
//    id school = data[@"school"];
//    [UserPreferences setSchooldId:[school[@"id"] unsignedIntegerValue]];
//    [UserPreferences setSchoolName:school[@"name"]];
//}
//
//+ (void)savePurgeData:(id)data {
//    NSUInteger64 now = [[NSDate date] timeIntervalSince1970];
//    NSInteger64  delta = now - [data safeIntegerForKey:@"current_time"];
//    NSUInteger64 endts = delta + [data safeIntegerForKey:@"end_time"] + 10;
//    if (endts < now) {
//        return;
//    }
//    NSUInteger64 begints = delta + [data safeIntegerForKey:@"start_time"] - 10;
//    [self setPurgeBegin:begints];
//    [self setPurgeEnd:endts];
//}
//
//+ (NSUInteger64)tsPurgeBegin {
//    return [[self preferenceValue:@"purge_begin"] unsignedLongLongValue];
//}
//
//+ (void)setPurgeBegin:(NSUInteger64)ts {
//    [self setPreferenceValue:@"purge_begin" value:@(ts)];
//}
//
//+ (NSUInteger64)tsPurgeEnd {
//    return [[self preferenceValue:@"purge_end"] unsignedLongLongValue];
//}
//
//+ (void)setPurgeEnd:(NSUInteger64)ts {
//    [self setPreferenceValue:@"purge_end" value:@(ts)];
//}
//
//+ (NSString*)userEmail {
//    NSString* t = [self preferenceValue:@"user_email"];
//    return t ? t : @"";
//}
//
//+ (void)setUserEmail:(NSString*)userEmail {
//    [self setPreferenceValue:@"user_email" value:userEmail];
//}
//
//+ (BOOL)noRemoteNotification {
//    return [[self preferenceValue:@"remote_notification"] boolValue];
//}
//
//+ (void)setNoRemoteNotification:(BOOL)value {
//    [self setPreferenceValue:@"remote_notification" value:@(value)];
//}
//
//+ (BOOL)noVibrateWithNewMessages {
//    return [[self preferenceValue:@"no_vibrate"] boolValue];
//}
//
//+ (void)setNoVibrateWithNewMessages:(BOOL)value {
//    [self setPreferenceValue:@"no_vibrate" value:@(value)];
//}
//
//+ (void)saveUnreadNotificationCount:(NSUInteger)count {
//    [self setPreferenceValue:@"unread_notification_count" value:@(count)];
//}
//
//+ (NSUInteger)unreadNotificationCount {
//    return [[self preferenceValue:@"unread_notification_count"] unsignedIntegerValue];
//}

@end
